
clear all

adopath ++/afs/umich.edu/group/m/mmcmps/ado

// MACROS
global funding "Y:/limited/Michigan_CTE/funding_change"



use "Y:/limited/Michigan_CTE/funding_change/data_final/psn_analysis.dta", clear 
keep if inrange(year,2012,2019)

gen cond_comp_rate = comp_rate/part_rate 
gen isd_flag = mod(dcode,1000)==0

gen mflag = inlist(., district_poor, district_city, district_town, district_rural)
recode district_poor district_city district_town district_rural (.=0)

foreach v of varlist hs_students cond_comp_rate any {
	bys psn: gegen pre_`v'  = mean(cond(year<2015,`v',.))
	bys psn: gegen post_`v' = mean(cond(year>=2017,`v',.))
	replace `v'=0 if `v'==.
}

egen tmp=sum(any) if year!=2015&year!=2016, by(psn)
egen n_part_psn=max(tmp), by(psn)
drop tmp

foreach v of any  cond_comp_rate {
	gen d_`v'=(post_`v'-pre_`v')*100
} 


keep if year==2014 & d_cond_comp_rate!=. //drop programs with no participants before or after
replace pre_hs_students = pre_hs_students/1000
 

* Counts for the paper
count
bys cipcode: gen n1 = _n
count if n1==1
bys dcode: gen n2= _n
count if n2==1

encode cipcode, gen(cip)

*reg mentioned in text
reg isd_flag district_city district_town district_rural district_poor pre_hs_students mflag if n2==1, r
		
eststo clear 
foreach y in d_cond_comp_rate  {
	eststo: reghdfe `y' isd_flag [aw=n_part_psn] ,   cl(dcode)  noabsorb
		estadd scalar ar2=`e(r2_a)' 
		sum `y' [aw=n_part_psn] if year==2014
		estadd scalar cm=`r(mean)'
		estadd scalar sd=`r(sd)'
		
	eststo: reghdfe `y' isd_flag [aw=n_part_psn] ,   cl(dcode)  a(cip)
		estadd scalar ar2=`e(r2_a)' 
		sum `y' [aw=n_part_psn] if year==2014
		estadd scalar cm=`r(mean)'
		estadd scalar sd=`r(sd)'	
		
	eststo: reghdfe `y' isd_flag district_city district_town district_rural district_poor pre_hs_students mflag [aw=n_part_psn] ,   cl(dcode)  a(cip)
		estadd scalar ar2=`e(r2_a)' 
		sum `y' [aw=n_part_psn] if year==2014
		estadd scalar cm=`r(mean)'
		estadd scalar sd=`r(sd)'	

		eststo: reghdfe `y' isd_flag district_city district_town district_rural district_poor pre_hs_students mflag [aw=n_part_psn] ,   cl(dcode)  a(cip cepd)
		estadd scalar ar2=`e(r2_a)' 
		sum `y' [aw=n_part_psn] if year==2014
		estadd scalar cm=`r(mean)'
		estadd scalar sd=`r(sd)'	
			
		
}

estout using Y:/limited/Michigan_CTE/funding_change/output/tab01_isd.txt,  drop(mflag) ///
	  cells(b(star fmt(3)) se(par(( )) fmt(3))) ///
	  stats(N cm sd ar2 Fcip pcip Fcepd pcepd, fmt(3)) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	  delimiter("," ) replace label
		
		
		
	/*	
bys cepd: egen isd_av = mean(isd_flag)
gen isd_avw = .
forval cepd = 1/54 {
	sum isd_flag [aw=n_part_psn] if cepd==`cepd'
	replace isd_avw = `r(mean)' if cepd==`cepd'
}

gen mostly_isd = isd_avw>=.5

